package com.shbaiche.bifenba.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.ui.match.MatchDetailActivity;

import java.util.List;

/**
 * FileName: HomeGridMatchAdapter
 * Author: 刘
 * Date: 2018/7/22 07:56
 * Description: ${DESCRIPTION}
 */
public class HomeGridMatchAdapter extends RecyclerView.Adapter<HomeGridMatchAdapter.Holder> {
    private Context context;
    List<MatchesVo> matchList;

    public HomeGridMatchAdapter(Context c, List<MatchesVo> matchList) {
        context = c;
        this.matchList = matchList;
    }

    public void setData(List<MatchesVo> matchList){
        this.matchList = matchList;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(context).inflate(R.layout.adapter_home_match, parent, false));
    }


    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        final MatchesVo item = matchList.get(position);
        holder.title.setText(item.getCompeteName() + "");
        if (item.getStatus() != null) {
            holder.state.setText(item.getStatus().getText() + "");
        }
        holder.oneName.setText(item.getHomeName() + "");
        holder.oneScore.setText(item.getHomeScore() + "");
        holder.twoName.setText(item.getVisitName() + "");
        holder.twoScore.setText(item.getVisitScore() + "");

        Glide.with(context).load(item.getHomeLogo()).placeholder(R.drawable.defult).error(R.drawable.ic_my_head).into(holder.oneImg);
        Glide.with(context).load(item.getVisitLogo()).placeholder(R.drawable.defult).error(R.drawable.ic_my_head).into(holder.twoImg);

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,MatchDetailActivity.class);
                intent.putExtra("data",item);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (matchList != null) {
            if (matchList.size() > 2) {
                return 2;
            } else {
                return matchList.size();
            }
        }
        return 0;
    }

    class Holder extends RecyclerView.ViewHolder {
        private TextView title, state, oneName, oneScore, twoName, twoScore;
        private ImageView oneImg, twoImg;
        private LinearLayout layout;

        public Holder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.match_name);
            state = (TextView) itemView.findViewById(R.id.match_state);
            oneName = (TextView) itemView.findViewById(R.id.match_one_name);
            oneScore = (TextView) itemView.findViewById(R.id.match_one_score);
            twoName = (TextView) itemView.findViewById(R.id.match_two_name);
            twoScore = (TextView) itemView.findViewById(R.id.match_two_score);
            oneImg = (ImageView) itemView.findViewById(R.id.match_one_img);
            twoImg = (ImageView) itemView.findViewById(R.id.match_two_img);
            layout = (LinearLayout) itemView.findViewById(R.id.home_match_layout);
        }


    }
}
